package find.prj.prog;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import javax.swing.SwingConstants;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

public class ListTester {

	private JFrame frmListtester;
	private JTextField txtFileName;
	private JSpinner txtArraySize;
	private JTextField txtMinRandom;
	private JTextField txtMaxRandom;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ListTester window = new ListTester();
					window.frmListtester.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ListTester() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmListtester = new JFrame();
		frmListtester.setTitle("ListTester");
		frmListtester.setBounds(100, 100, 563, 441);
		frmListtester.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmListtester.getContentPane().setLayout(null);
		
		JButton btnCreateFile = new JButton("Create file");
		btnCreateFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int arraySize = new Integer(txtArraySize.getValue().toString());
				int minRandom = new Integer(txtMinRandom.getText());
				int maxRandom = new Integer(txtMaxRandom.getText());
				String intsString = RandomIntString(minRandom, maxRandom, arraySize);
				
				BufferedWriter writer = null;
				try
				{
					writer = new BufferedWriter( new FileWriter(txtFileName.getText()));
					writer.write(intsString);
				}
				catch ( IOException exception)
				{
				}
				finally
				{
					try
					{
						if ( writer != null)
							writer.close( );
					}
					catch ( IOException exception)
					{
					}
			     }

			}
		});
		btnCreateFile.setBounds(10, 356, 220, 36);
		frmListtester.getContentPane().add(btnCreateFile);
		
		JLabel lblArrayDimension = new JLabel("Array size");
		lblArrayDimension.setBounds(10, 67, 220, 14);
		frmListtester.getContentPane().add(lblArrayDimension);
		
		txtArraySize = new JSpinner();
		txtArraySize.setModel(new SpinnerNumberModel(new Integer(1000), null, null, new Integer(1)));
		txtArraySize.setBounds(10, 92, 120, 20);
		frmListtester.getContentPane().add(txtArraySize);
		
		JLabel lblRandomArrayFile = new JLabel("Random array file name");
		lblRandomArrayFile.setBounds(10, 11, 220, 14);
		frmListtester.getContentPane().add(lblRandomArrayFile);
		
		txtFileName = new JTextField();
		txtFileName.setText("D:\\Supa\\workspace\\mega-test-project\\FindProject\\1.txt");
		txtFileName.setBounds(10, 36, 220, 20);
		frmListtester.getContentPane().add(txtFileName);
		txtFileName.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Min random int");
		lblNewLabel.setBounds(10, 123, 110, 14);
		frmListtester.getContentPane().add(lblNewLabel);
		
		txtMinRandom = new JTextField();
		txtMinRandom.setHorizontalAlignment(SwingConstants.RIGHT);
		txtMinRandom.setText("1");
		txtMinRandom.setBounds(10, 148, 86, 20);
		frmListtester.getContentPane().add(txtMinRandom);
		txtMinRandom.setColumns(10);
		
		txtMaxRandom = new JTextField();
		txtMaxRandom.setText("1000");
		txtMaxRandom.setHorizontalAlignment(SwingConstants.RIGHT);
		txtMaxRandom.setBounds(144, 148, 86, 20);
		frmListtester.getContentPane().add(txtMaxRandom);
		txtMaxRandom.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Max random int");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setBounds(120, 123, 110, 14);
		frmListtester.getContentPane().add(lblNewLabel_1);
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.setBounds(359, 35, 89, 23);
		frmListtester.getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("New button");
		btnNewButton_1.setBounds(359, 147, 89, 23);
		frmListtester.getContentPane().add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("New button");
		btnNewButton_2.setBounds(359, 290, 89, 23);
		frmListtester.getContentPane().add(btnNewButton_2);
		
		JLabel lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setBounds(359, 95, 46, 14);
		frmListtester.getContentPane().add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("New label");
		lblNewLabel_3.setBounds(359, 221, 46, 14);
		frmListtester.getContentPane().add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("New label");
		lblNewLabel_4.setBounds(359, 356, 46, 14);
		frmListtester.getContentPane().add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("New label");
		lblNewLabel_5.setBounds(10, 221, 46, 14);
		frmListtester.getContentPane().add(lblNewLabel_5);
	}
	
	String RandomIntString (int min, int max, int count) {
		Random random = new Random();
		StringBuilder outString = new StringBuilder("");
		int range = max - min + 1;
		for (int i = 1; i <= count; i++)
			outString.append( (random.nextInt(range) + min) + "\r\n");
		return outString.toString().trim();
	}
}
